﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LibrarySolution.ComMethod;
using LibrarySolution.DAO.委托;
using LibrarySolution.DAO.基础;
using LibrarySolution.Model.基础;

namespace LibrarySolution.Frm
{
    public partial class FrmJS : Form
    {
        #region 属性
        String yhNo;
        JHBkDao jsDao =new JHBkDao();
        JHBkDao jhbkDao = new JHBkDao();
        JSRecord jsRecord;
        public DataTable datatable;
        YhDao yhDao;
        private static FrmJS Instance;
        Delegates delegates = new Delegates();
        #endregion

        #region 构造器
        public FrmJS()
        {
            InitializeComponent();
            delegates.refreshLBEvent += new Delegates.refreshLBEventHandler(delegates_refreshLBEvent);
            UCDataQuery.DataQuery +=new ComControl.UCDataQuery.dataQueryEventHandler(UCDataQuery_DataQuery);
        }
        #endregion

        #region 事件
        public static FrmJS GetInstance()
        {

            Instance = new FrmJS();
            Instance.jsRecord = new JSRecord();
            if (GlobalVar.YhEnter == true)
            {
                Instance.toolStrip1.Items[0].Visible = false;
                Instance.toolStrip1.Items[1].Visible = false;
                Instance.toolStrip1.Items[2].Enabled = true;
                Instance.toolStrip1.Items[3].Enabled = true;
                Instance.toolStrip1.Items[4].Visible = false;
                Instance.TxtQueryByYhNo.Visible = false;
                Instance.jsRecord.js_yhno = GlobalVar.YhxxEntity.yh_no;
                Instance.BindingMyBook();
            }
            Instance.MdiParent = GlobalVar.frmMain;
            GlobalVar.frmMain.panel1.Controls.Add(Instance);
            GlobalVar.FrmJs = Instance;
            Instance.BindingData();
            Instance.BringToFront();
            return Instance;
        }

        private void TstripBtnRefresh_Click(object sender, EventArgs e)
        {
            BindingData();
            BindingMyBook();
        }

        private void TstripBtnSearch_Click(object sender, EventArgs e)
        {
            for (int i = 1; i < toolStrip1.Items.Count - 1; i++)
            {
                toolStrip1.Items[i].Enabled = false;
            }
            String yhNo;
            yhDao = new YhDao();
            yhNo = this.TxtQueryByYhNo.Text;
            GlobalVar.YhxxEntity = yhDao.getYhxxByNo(yhNo);
            if (ifyhexist())
            {
                BindingData();
                BindingMyBook();
                if (ifgivebake()) ifpayfree();
            }
            if (GlobalVar.failtopay) return;
            for (int i = 1; i < toolStrip1.Items.Count - 1; i++)
            {
                toolStrip1.Items[i].Enabled = true;
            }
        }

        private void TstripBtnJS_Click(object sender, EventArgs e)
        {
            if (int.Parse(GlobalVar.YhxxEntity.yh_yjsl) == 20)
            {
                MessageBox.Show("您已借书20本,不能再借");
                return;
            }
            if (UCDataQuery.rowDataBase() == null) return;
            String bkName, bkNo;
            bkName = UCDataQuery.rowDataBase()[1];
            bkNo = UCDataQuery.rowDataBase()[0];
            if (MessageBox.Show("您要借的书是《" + bkName + "》?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation,
                MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.Yes)
            {
                jsRecord = new JSRecord();
                GlobalVar.YhxxEntity.yh_xjsl = GlobalVar.YhxxEntity.yh_xjsl;
                GlobalVar.YhxxEntity.yh_yjsl = (int.Parse(GlobalVar.YhxxEntity.yh_yjsl) + 1).ToString();
                jsRecord.js_ghrq = DataUtil.addTime(GlobalVar.YhxxEntity.jyqx);
                jsRecord.js_yhno = GlobalVar.YhxxEntity.yh_no;
                jsRecord.js_tsbh = bkNo;
                jsRecord.js_yhxj = GlobalVar.YhxxEntity.yh_xjsl;
                jsRecord.js_yhyj = GlobalVar.YhxxEntity.yh_yjsl;
                jsRecord.js_time = DataUtil.getTime();
                jsDao.insertJs(jsRecord);
                BindingMyBook();
                jsRecord = null;
                BindingData();
            }
        }

        private void TstripBtnXJ_Click(object sender, EventArgs e)
        {
            String bkName, jlCode, oldTime;
            if (dataGridView1.Rows.Count == 0)
                return;
            bkName = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
            jlCode = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
            oldTime = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
            if (MessageBox.Show("您要续借的书是《" + bkName + "》?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation,
                MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.Yes)
            {
                if (DateTime.Now > DateTime.Parse(oldTime))
                {
                    MessageBox.Show("续借失败,超过续借期限!");
                    return;
                }
                jsRecord = new JSRecord();
                jsRecord.js_ghrq = DataUtil.turnTime(oldTime,GlobalVar.YhxxEntity.xjqx);
                jsRecord.js_code = jlCode;
                jsRecord.js_yhno = GlobalVar.YhxxEntity.yh_no;
                jsDao.updateXjxx(jsRecord);
                MessageBox.Show("续借成功");
                BindingMyBook();
            }
        }

        private void TstripKf_Click(object sender, EventArgs e)
        {
            if (GlobalVar.YhxxEntity.yh_no != null)
            {
                FrmKfxx.isModify = false;
                if (datatable == null || datatable.Rows.Count == 0)
                {
                    MessageBox.Show("您的账号未欠费!");
                    return;
                }
                FrmKfxx.GetInstance(datatable).ShowDialog();
            }
            else
                FrmKfxx.GetInstance().ShowDialog();
        }

        private void FrmJS_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!GlobalVar.YhEnter)
                GlobalVar.YhxxEntity.yh_no = null;
        }

        private void TxtQueryByBkAuthor_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                DataTable dt = null;
                jsRecord = new JSRecord();
                jsRecord.js_writer = TxtQueryByBkAuthor.Text.Trim();
                dt = jsDao.getBkxxTable(jsRecord);
                if (dt == null || dt.Rows.Count == 0)
                    MessageBox.Show("输入有误或书本不存在或已外借!");
                else
                {
                    UCDataQuery.Datasource = dt;
                    UCDataQuery.Refresh();
                }
                TxtQueryByBkAuthor.Text = "";
            }
        }

        private void TxtQueryByBkName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                jsRecord = new JSRecord();
                jsRecord.js_tsmc = TxtQueryByBkName.Text.Trim();
                DataTable dt = null;
                dt = jsDao.getBkxxTable(jsRecord);
                if (dt == null || dt.Rows.Count == 0)
                    MessageBox.Show("输入有误或书本不存在或已外借!");
                else
                {
                    UCDataQuery.Datasource = dt;
                    UCDataQuery.Refresh();
                }
                TxtQueryByBkName.Text = "";
            }
        }

        private void TxtQueryByBkNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                jsRecord = new JSRecord();
                jsRecord.js_tsbh = TxtQueryByBkNo.Text.Trim();
                DataTable dt = null;
                dt = jsDao.getBkxxTable(jsRecord);
                if (dt == null || dt.Rows.Count == 0)
                    MessageBox.Show("输入有误或书本不存在或已外借!");
                else
                {
                    UCDataQuery.Datasource = dt;
                    UCDataQuery.Refresh();
                }
                TxtQueryByBkNo.Text = "";
            }
        }
        #endregion

        #region 方法
        void BindingData()
        {
            DataTable datetable;
            if (jsRecord == null)
                jsRecord = new JSRecord();
            yhDao = new YhDao();
            yhNo = GlobalVar.YhxxEntity.yh_no;
            GlobalVar.YhxxEntity = yhDao.getYhxxByNo(yhNo);
            LblYhName.Text = "用 户 名:" + GlobalVar.YhxxEntity.yh_name;
            LblYhXjsl.Text = "续借次数:" + GlobalVar.YhxxEntity.yh_xjsl;
            LblYhYjsl.Text = "已借数量:" + GlobalVar.YhxxEntity.yh_yjsl;
            LblYhjb.Text = "用户级别:" + GlobalVar.YhxxEntity.yh_JB;
            LblJyqx.Text = "借阅期限:" + GlobalVar.YhxxEntity.jyqx + "个月";
            LblXjqx.Text = "续借期限:" + GlobalVar.YhxxEntity.xjqx + "天";
            groupBox1.Text = "用户编号:" + GlobalVar.YhxxEntity.yh_no;
            LblYhye.Text = "用户余额:" + GlobalVar.YhxxEntity.yh_zhye + "元";
            datetable = jsDao.getBkxxTable(jsRecord);
            UCDataQuery.Datasource = datetable;
            UCDataQuery.Refresh();
        }

        DataTable  UCDataQuery_DataQuery()
        {
            JSRecord jsRecord = new JSRecord();
            jsRecord.js_tsmc = ComControl.UCDataQuery.Query;
            return jsDao.getBkTbByQuery(jsRecord);
        }

        void delegates_refreshLBEvent()
        {            
            BindingData();
            try
            {
                BindingMyBook();
            }
            catch { }
        }

        public Boolean ifyhexist()
        {
            jsRecord = new JSRecord();
            jsRecord.js_yhno = GlobalVar.YhxxEntity.yh_no;
            if (GlobalVar.YhxxEntity.yh_no == null)
            {
                MessageBox.Show("请输入正确的用户编号!");
                return false;
            }
            else
                return true;
        }

        public Boolean ifgivebake()
        {
            Int32 num;
            Double money = 0.0, sum = 0.0;
            String days;
            DataRow dataRow;
            DataTable dataTable;
            dataTable = jhbkDao.getTsjl(jsRecord);
            num = dataTable.Rows.Count;
            if (num == 0)
            {
                GlobalVar.failtopay = false;
                return false;
            }
                setdataTable();
                for (int i = 0; i < num; i++)
                {
                    DateTime dt = DateTime.Parse(dataTable.Rows[i][8].ToString());
                    if (dt < DateTime.Now && dataTable.Rows[i][10].ToString() == "0")
                    {
                        if (dataTable.Rows[i][0].ToString() == "False")
                        {
                            MessageBox.Show("您所借图书已超期,请先归还!");
                            GlobalVar.failtopay = true;
                            return false;
                        }
                        days = (DateTime.Parse(dataTable.Rows[i][9].ToString()) - dt).ToString("dd");
                        money = Convert.ToInt32(days) * 0.1;
                        sum += money;
                        dataRow = datatable.NewRow();
                        dataRow[0] = dataTable.Rows[i][2].ToString();
                        dataRow[1] = dataTable.Rows[i][3].ToString();
                        dataRow[2] = GlobalVar.YhxxEntity.yh_no;
                        dataRow[3] = GlobalVar.YhxxEntity.yh_name;
                        dataRow[4] = dt.ToString("yyyy-MM-dd");
                        dataRow[5] = days;
                        dataRow[6] = Convert.ToString(money);
                        dataRow[7] = GlobalVar.GlyEntity.gly_name;
                        dataRow[8] = DateTime.Now.ToString("yyyy-MM-dd");
                        datatable.Rows.Add(dataRow);
                    }
                }
                dataRow = datatable.NewRow();
                datatable.Rows.Add(dataRow);
                dataRow[0] = "合计:";
                dataRow[6] = sum;
                if (datatable.Rows.Count > 1)
                    return true;
                else
                {
                    GlobalVar.failtopay = false;
                    return false;
                }
        }

        public void  ifpayfree()
        {
            GlobalVar.failtopay = true;
            if (MessageBox.Show("您要结清超期的金额?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation,
            MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.Yes)
            {
                if (FrmKfxx.isModify)
                {
                    FrmKfxx.GetInstance(datatable).ShowDialog();
                    if (GlobalVar.failtopay == false) datatable = null;
                }
            }
        }

        void setdataTable()
        {
            datatable = new DataTable();
            datatable.Columns.Add("图书编号");
            datatable.Columns.Add("图书名称");
            datatable.Columns.Add("用户编号");
            datatable.Columns.Add("用户名称");
            datatable.Columns.Add("归还日期");
            datatable.Columns.Add("超期天数");
            datatable.Columns.Add("扣除费用");
            datatable.Columns.Add("操作员名称");
            datatable.Columns.Add("操作日期");
        }

        void BindingMyBook()
        {
            dataGridView1.DataSource = jhbkDao.getMybook(jsRecord);
            dataGridView1.Columns["记录编码"].Visible = false;
            dataGridView1.Refresh();
        }
        #endregion

    }
}
